<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>TOUPROPRE DBMS - Browser</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>Browser</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
<?php 
/*
 * Includes
 */
include "tools/ColumnNames.php";
include "tools/Forms.php";
include "tools/Queries.php";

/*
 * Function Definitions
 */
function ShowNewQueryLink() {
    $add="?table=".$_GET['table'];     
    echo " | <a href=browse.php".$add.">New Query</a>";
}
function ShowSelectTable() {
    echo "<a href=browse.php>Select Table</a>";
}
function ShowTableSelectForm() {
    echo "<p class='text'>This page will allow you to browse through the database.</p>";
    echo "\n<p class='text'>Please select a table:</p>";
    echo "\n<form action='browse.php' method='get'>";
    echo "\n<select name='table'>";
    echo "\n<option value='clients'>Clients</option>";
    echo "\n<option value='suppliers'>Suppliers</option>";
    echo "\n<option value='items'>Items</option>";
    // TODO : Cas particulier pour ces trois-ci :
    echo "\n<option value='sales'>Sales</option>";
    echo "\n<option value='purchases'>Purchases</option>";
    echo "\n<option value='facilities'>Facilities</option>";
    echo "\n<option value='itemStock'>Stock</option>";
    echo "\n</select>";
    echo "\n<input type='submit' name='submit' value='Select'>";
    echo "\n</form>";
}
function BuildQuery() {
    $columns = GetColumnNames($_GET['table'], false);
    $sql = "SELECT * FROM ".$_GET['table']." WHERE (";
    
    $addand=false;
    $n = count($columns);
    for($i=0; $i<$n; $i++) {
        if( $_POST[$columns[$i]] != '' ) {
            if($addand) { $sql = $sql.' AND '; }
            $sql = $sql.$columns[$i]."='".$_POST[$columns[$i]]."'";
            $addand = true;
        }
    }
    $sql = $sql . ')';

    return $sql;
}

/*
 * Behaviour :
 */
if(isset($_GET['table'])) {
    if(isset($_POST['submit'])) {
        // Build Query and Display :
        if($_POST['submit'] == 'Search') {
            $sql = BuildQuery();
        }
        else {
            $sql = "SELECT * FROM ".$_GET['table'];
        }
        
        switch($_GET['table']) {
        case 'purchases':
            EchoSelectQuery_items($sql, 'itemsPurchased');
            break;
        case 'sales':
            EchoSelectQuery_items($sql, 'itemsSold');
            break;
        default:
            EchoSelectQuery($sql);
            break;
        }
        
        ShowSelectTable(); ShowNewQueryLink();
    }
    else {
        EchoTableForm('browse.php', 'submit', 'Search', $_GET['table'], true);
        echo "<form action='browse.php?table=".$_GET['table']."' method='post'>";
        echo "<input type='submit' name='submit' value='Select All'/>";
        echo "</form><br/><br/>";
        ShowSelectTable();
    }
 }
 else {
     ShowTableSelectForm();
 }
?>
    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>
